// 封装的全局工具

// 提示框
export const toast = (title) => {
	uni.showToast({
		title: title,
		duration: 1000,
		mask: true,
	});
}

// loading框
export const load = (title) => {
	uni.showLoading({
		title: title,
		mask: true,
	});
}

// 预览图片
export const preImgs = ({
	url = '',
	index = 0
} = {}) => {
	uni.previewImage({
		urls: url, 
		current: index,
		longPressActions: {
			itemList: ['发送给朋友', '保存图片', '收藏'],
			success: function(data) {
				// console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
			},
			fail: function(err) {
				// console.log(err.errMsg);
				uni.showToast({
					title: '预览失败',
					duration:1000,
					mask: false,
				})
			}
		}
	});
}



// 单独封装一个修改头像的api
// 单独上传头像
export const uploadava = () => {
	return new Promise((resolve, reject) => {
		// debugger
		uni.chooseImage({
			count: 1,
			success: (chooseImageRes) => {
				const tempFilePaths = chooseImageRes.tempFilePaths;
				console.log('选择的图片', tempFilePaths);
				uni.uploadFile({
					url: imgUrl(), //仅为示例，非真实的接口地址
					filePath: tempFilePaths[0],
					name: 'file',
					success: (uploadFileRes) => {

						console.log('上传', uploadFileRes);
						if (uploadFileRes?.statusCode >= 200 && uploadFileRes
							?.statusCode < 300) {
							resolve(uploadFileRes.data)
						} else if (uploadFileRes?.statusCode == 401) {
							// 没有登录或是登录失效，根据项目不同进行处理
							reject()
						} else if (uploadFileRes?.statusCode == 403) {
							uni.showToast({
								title: '您没有权限',
								duration: 1000,
								mask: false,
							})
							reject()
						} else {
							uni.showToast({
								title: uploadFileRes.errMsg,
								icon: 'none',
								duration: 1000,
								mask: false,
							})
							reject()
						}
					},
					fail() {
						reject()
					}
				});
			},

			fail(err) {
				console.log(err);
			}
		});
	})
}




 